package com.Dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author:xuyuanhong
 * @Date:Created in 2022/12/11 16:33
 */
public class MYconnection {
    //jdbc
    //设置变量 数据库账户名 密码等

    private static String name="root";
    private static String pssword="mmm18623908829";
    private static String jdbc_driver="com.mysql.cj.jdbc.Driver";

    //url
    private static String url = "jdbc:mysql://localhost:3306/echarts?useUnicode=true&characterEncoding=utf-8";




    private static Connection connection = null;
    private static Statement stmt = null;

    public static void init() throws ClassNotFoundException {
        try{
//            注册驱动
            Class.forName(jdbc_driver);
//            连接hive
            connection= DriverManager.getConnection(url,name,pssword);
//             建立连接
            stmt = connection.createStatement();
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    public static void close(Statement statement, Connection connection){
        // 编译 的连接  ；执行sql 关闭
        if(statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        // mysql 连接   ； 关掉
        if(connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }



}
